package com.zisuyi.uilibrary.uitils;

import android.content.ComponentName;
import android.content.Context;
import android.content.Intent;
import android.content.pm.ActivityInfo;
import android.content.pm.ResolveInfo;
import android.net.Uri;
import android.text.TextUtils;

import java.util.List;

public class GooglePlayUtil {
    private static final String PLAY_PACKAGENAME = "com.android.vending";
    private static final String PLAY_HUAWEI = "com.huawei.appmarket";
    public static final String PLAY_DETAILS_PACK_PREFIX = "market://details?id=";

    /**
     * 默认加入当前包名为统计关键字
     *
     * @param context
     * @param packName
     * @return
     */
    public static Intent openAppInPlay(Context context, String packName) {
        // 为了防止google play关联，不能直接用packageName，需要对它进行改造一下
        String utm_source = context.getPackageName();
        if (utm_source.length() > 4) {
            utm_source = utm_source.substring(4, utm_source.length());
        }
        utm_source = utm_source.replace('.', '_');
        packName = packName == null ? context.getPackageName() : (packName + "&referrer=utm_source%3D" + utm_source);
        Intent rateIntent = new Intent(Intent.ACTION_VIEW, Uri.parse(PLAY_DETAILS_PACK_PREFIX + packName));
        rateIntent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK | Intent.FLAG_ACTIVITY_RESET_TASK_IF_NEEDED | Intent.FLAG_ACTIVITY_CLEAR_TOP);
        // find all applications able to handle our rateIntent
        final List<ResolveInfo> otherApps = context.getPackageManager().queryIntentActivities(rateIntent, 0);
        for (ResolveInfo otherApp : otherApps) {
            // look for Google Play application
            if (TextUtils.equals(otherApp.activityInfo.applicationInfo.packageName, PLAY_PACKAGENAME)) {

                ActivityInfo otherAppActivity = otherApp.activityInfo;
                ComponentName componentName = new ComponentName(
                        otherAppActivity.applicationInfo.packageName,
                        otherAppActivity.name
                );
                rateIntent.setComponent(componentName);
                context.startActivity(rateIntent);
                return rateIntent;
            }
        }
        context.startActivity(rateIntent);
        return rateIntent;
    }

    /**
     * 启动到应用商店app详情界面
     *
     * @param appPkg    目标App的包名
     * @param marketPkg 应用商店包名 ,如果为""则由系统弹出应用商店列表供用户选择,否则调转到目标市场的应用详情界面，某些应用商店可能会失败
     */
    public static void launchAppDetail(Context context, String appPkg, String marketPkg) {
        if (TextUtils.isEmpty(appPkg)) return;
        Uri uri = Uri.parse("market://details?id=" + appPkg);
        Intent intent = new Intent(Intent.ACTION_VIEW, uri);
        if (!TextUtils.isEmpty(marketPkg)) {
            intent.setPackage(marketPkg);
        }
        intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
        context.startActivity(intent);
    }

    public static void launchHuawei(Context context, String appPkg) {
        if (ShareUtilsKt.isAppInstalled(context, PLAY_HUAWEI)) {
            launchAppDetail(context, appPkg, PLAY_HUAWEI);
        } else {
            launchAppDetail(context, appPkg, null);
        }
    }
}
